package com.lianxi.nmms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lianxi.nmms.constant.DictConstant;
import com.lianxi.nmms.constant.StatusConstant;
import com.lianxi.nmms.controller.ProductController;
import com.lianxi.nmms.dao.ProductTypeMapper;
import com.lianxi.nmms.entity.ProductType;
import com.lianxi.nmms.entity.ProductTypeExample;
import com.lianxi.nmms.exception.ProductTypeExistException;
import com.lianxi.nmms.exception.RequestParameterErrorException;
import com.lianxi.nmms.service.ProductTypeService;
import com.lianxi.nmms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 翔哥
 * 2025年09月28日
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper typeMapper;


    @Override
    public void addType(String name) throws RequestParameterErrorException, ProductTypeExistException {
        if(ParameterUtil.isNull(name)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        ProductTypeExample example =new ProductTypeExample();
        example.or()
                .andNameEqualTo(name);
        List<ProductType> types =typeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new ProductTypeExistException("该商品类别已经存在了");
        }

        ProductType type =new ProductType();
        type.setName(name);
        type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        typeMapper.insertSelective(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<ProductType> findPage(String pageNo, String pageSize) {

        if (ParameterUtil.isNull(pageNo)){
            pageNo = DictConstant.PRODUCT_TYPE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)){
            pageSize = DictConstant.PRODUCT_TYPE_DEFAULT_PAGE_SIZE;
        }

        PageHelper.startPage(Integer.valueOf(pageNo),Integer.valueOf(pageSize));

        List<ProductType> types = typeMapper.selectByExample(new ProductTypeExample());

        PageInfo<ProductType> typePage = new PageInfo<>(types);
        return typePage;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public ProductType findById(String id) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) ) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        ProductTypeExample example =new ProductTypeExample();
        example.or()
                .andIdEqualTo(Integer.valueOf(id));
        List<ProductType> types =typeMapper.selectByExample(example);
        ProductType productType =types.get(0);
        return productType;
    }

    @Override
    public void updateName(String id, String name) throws RequestParameterErrorException, ProductTypeExistException {
        if(ParameterUtil.isNull(name)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        ProductTypeExample example =new ProductTypeExample();
        example.or()
                .andNameEqualTo(name)
                .andIdNotEqualTo(Integer.valueOf(id));
        List<ProductType> types =typeMapper.selectByExample(example);
        if(!types.isEmpty()){
            throw new ProductTypeExistException("该商品已经存在了");
        }
        ProductType productType =new ProductType();
        productType.setId(Integer.valueOf(id));
        productType.setName(name);

        typeMapper.updateByPrimaryKeySelective(productType);

    }


    @Override
    public void updateStatus(String id ,String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }

        ProductType productType = findById(id);

        if(productType.getStatus() == StatusConstant.PRODUCT_TYPE_STATUS_ENABLED){
            productType.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_DISABLED);
        }else {
            productType.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        }
        typeMapper.updateByPrimaryKeySelective(productType);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<ProductType> findAll() {
        ProductTypeExample productTypeExample =new ProductTypeExample();

        return typeMapper.selectByExample(productTypeExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<ProductType> findStatus() {

        ProductTypeExample productTypeExample =new ProductTypeExample();
        productTypeExample.or()
                .andStatusEqualTo(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        return typeMapper.selectByExample(productTypeExample);
    }
}
